//
//  UpdatePasswordViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/6/30.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "UpdatePasswordViewController.h"

@interface UpdatePasswordViewController ()

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, weak)UIView * backTopView;

@property (nonatomic, weak)UILabel * oldPasswordLabel;

@property (nonatomic, weak)UITextField * oldPasswordTextFiled;

@property (nonatomic, weak)UIView * backBottomView;

@property (nonatomic, weak)UILabel * npsLabel;

@property (nonatomic, weak)UITextField * npsTextFiled;

@property (nonatomic, weak)UILabel * explationLabel;

@property (nonatomic, weak)UIButton * saveButton;

@end

@implementation UpdatePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}

- (void)setupNavBar{
    
    self.title = @"修改密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    UIView * backTopView = [UIView new];
    
    backTopView.backgroundColor = [UIColor whiteColor];
    backTopView.alpha = 0.05;
    
    self.backTopView = backTopView;
    
    [self.view addSubview:self.backTopView];
    
    
    UILabel * oldPasswordLabel = [[UILabel alloc] init];
    
    oldPasswordLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    oldPasswordLabel.textColor = ColorWithRGB(0xffffff);
    oldPasswordLabel.text = @"密码";
    
    self.oldPasswordLabel = oldPasswordLabel;
    
    [self.view addSubview:self.oldPasswordLabel];
    
    
    UITextField * oldPasswordTextFiled = [[UITextField alloc] init];
    
    oldPasswordTextFiled.borderStyle = UITextBorderStyleNone;
    oldPasswordTextFiled.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    oldPasswordTextFiled.tintColor = ColorWithRGB(0x53565c);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x53565c);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    oldPasswordTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入旧密码" attributes:attrs];
    oldPasswordTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    oldPasswordTextFiled.textColor = ColorWithRGB(0x53565c);
    oldPasswordTextFiled.keyboardType = UIKeyboardTypeDefault;
    oldPasswordTextFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    oldPasswordTextFiled.secureTextEntry = YES;
    
    self.oldPasswordTextFiled = oldPasswordTextFiled;
    
    [self.view addSubview:self.oldPasswordTextFiled];
    
    
    UIView * backBottomView = [UIView new];
    
    backBottomView.backgroundColor = [UIColor whiteColor];
    backBottomView.alpha = 0.05;
    
    self.backBottomView = backBottomView;
    
    [self.view addSubview:self.backBottomView];
    
    
    UILabel * newPasswordLabel = [[UILabel alloc] init];
    
    newPasswordLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    newPasswordLabel.textColor = ColorWithRGB(0xffffff);
    newPasswordLabel.text = @"新密码";
    
    self.npsLabel = newPasswordLabel;
    
    [self.view addSubview:self.npsLabel];
    
    
    UITextField * newPasswordTextFiled = [[UITextField alloc] init];
    
    newPasswordTextFiled.borderStyle = UITextBorderStyleNone;
    newPasswordTextFiled.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    newPasswordTextFiled.tintColor = ColorWithRGB(0x53565c);
    
    newPasswordTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入新的密码" attributes:attrs];
    newPasswordTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    newPasswordTextFiled.textColor = ColorWithRGB(0x53565c);
    newPasswordTextFiled.keyboardType = UIKeyboardTypeDefault;
    newPasswordTextFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    newPasswordTextFiled.secureTextEntry = YES;
    
    self.npsTextFiled = newPasswordTextFiled;
    
    [self.view addSubview:self.npsTextFiled];
    
    
    UILabel * explationLabel = [[UILabel alloc] init];
    
    explationLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    explationLabel.textColor = ColorWithRGB(0xee4b4e);
    explationLabel.text = @"*8-20位，并包括大小写字母及数字(特殊字符除外)";
    
    self.explationLabel = explationLabel;
    
    [self.view addSubview:self.explationLabel];
    
    
    UIButton * saveButton = [[UIButton alloc] init];
    
    [saveButton setTitle:@"提交" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16 * SCREENH6];//
    [saveButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    saveButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    //    saveButton.layer.borderWidth =1.0f;
    //处理圆角
    saveButton.layer.cornerRadius =5.0f;
    [saveButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [saveButton addTarget:self action:@selector(didSaveButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.saveButton = saveButton;
    
    [self.view addSubview:self.saveButton];
}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.top.mas_equalTo(self.view);
    }];
    
    [self.backTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(82 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.oldPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backTopView);
        make.leading.mas_equalTo(self.backTopView).offset(10 * SCREENH6);
    }];
    
    [self.oldPasswordTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backTopView);
        make.leading.mas_equalTo(self.oldPasswordLabel.mas_trailing).offset(45 * SCREENH6);
        make.height.mas_equalTo(self.oldPasswordLabel);
        make.width.mas_equalTo(150 * SCREENH6);
    }];
    
    [self.backBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backTopView.mas_bottom).offset(14 * SCREENH6);
        make.leading.trailing.height.mas_equalTo(self.backTopView);
    }];
    
    [self.npsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backBottomView);
        make.leading.mas_equalTo(self.backBottomView).offset(10 * SCREENH6);
    }];
    
    [self.npsTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backBottomView);
        make.height.width.leading.mas_equalTo(self.oldPasswordTextFiled);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backBottomView.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.backBottomView);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.explationLabel.mas_bottom).offset(40 * SCREENH6);
        make.leading.trailing.mas_equalTo(self.backBottomView);
        make.height.mas_equalTo(45 * SCREENH6);
    }];
}



- (void)didSaveButton{
    
    [self.oldPasswordTextFiled resignFirstResponder];
    [self.npsTextFiled resignFirstResponder];
    
    if ([self.oldPasswordTextFiled.text isEqualToString:@""] || [self.npsTextFiled.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"密码不能为空,请输入密码"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        NSDictionary * parametersDic = @{@"password":self.oldPasswordTextFiled.text,@"newPassword":self.npsTextFiled.text};
        
        [[NetworkTool sharedTools] POST:@"user/edit-pass" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
            
            if ([code isEqualToString:@"0"]) {
                
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                
                [userDefaults setObject:self.npsTextFiled.text forKey:@"password"];
                
                [userDefaults synchronize];
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你，修改密码成功！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          //响应事件
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSLog(@"%@",error);
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"网络连接失败！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }];
        
    }
  
}



- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
